<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Callback extends CI_Controller {

    function __construct() {
        parent::__construct();

        $this->load->model('Query_builder_model', 'qb');
        $this->load->library('File_manipulate', '', 'fm');
    }

    public function index($controller='', $action='', $user=0) {

        $param = $this->input->post('param');

        if (!$param) {
            $param = '{"controller":"' . $controller . '", "page":"0", "action":"' . $action . '"}';
        }

        $res = json_decode($param);

        $json = array(
            'redirect' => 1,
            'url' => BASE_URL . $res->controller . '/index/list/' . ($res->page + 1),
            'message' => 'Data delated!',
            'controller' => $res->controller
        );


        switch ($res->action) {
            case 'delete':
                $ids = json_decode($res->ids);
                $this->qb->delete(array('from' => $res->controller, 'key' => $ids, 'field' => 'id'));
                $message = (isset($GLOBALS['messages'][$res->controller]['delete'])) ? $GLOBALS['messages'][$res->controller]['delete'] : 'Data(s) Deleted';
                $this->session->set_userdata(array('alerts' => array('message' => $message, 'type' => 'info')));
                break;
            case 'upload':
                $this->fm->upload($user);
                break;
            case 'list':
                $json['html'];
                break;
            default :
                break;
        }
        echo json_encode($json);
    }

    function banner($id=9) {


        if (is_numeric($id)) {
            $id = md5($id);
        }

        $res = $this->qb->select(array('from' => 'banners', 'where' => array('hash' => $id), 'limit' => 1));

        $data = $res->row_array();

        $attr = array();

        foreach (explode(',', $data['content']) as $v) {
            $array = explode('_', $v);
            $res = $this->qb->select(array('from' => ($array[0]=='page')?'pages':'files', 'where' => array('id' => floor($array[1])), 'limit' => 1));
            if ($res) {
                $res = $res->row_array();
                $attr[$array[0]][$array[1]] = $res;
            }
        }

        $this->load->view('extras/banner_rotator', array('data' => $data, 'attr' => $attr));
    }

    function pic($id=1, $height=281, $width=0) {

        $query = array(
            'from' => 'files',
            'where' => array('type' => 'picture', 'id' => $id),
            'limit' => 1,
        );

        $source_image = DOC_ROOT . 'imports/images/default.jpg';

        $res = $this->qb->select($query);

        if ($res) {
            $data = $res->row_array();
            $source_image = DOC_ROOT . $data['location'];
        }

        $param = array(
            'source_image' => $source_image,
            'height' => $height
        );
        if (!$height)
            unset($param['height']);
        if ($width)
            $param['width'] = $width;

        echo $this->fm->img_resize($param);
    }

    function img($id) {
        $query = array(
            'from' => 'files',
            'where' => array('type' => 'picture', 'id' => $id),
            'limit' => 1,
        );

        $source_image = DOC_ROOT . 'imports/images/default.jpg';

        $res = $this->qb->select($query);

        $data = array('msg' => 'error');

        if ($res) {
            $data = $res->row_array();
        }
        echo json_encode($data);
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */